from ast import literal_eval
import random
import os

def replace_dataset_symbol(list, newElement, oldElement=u'[dataset]'):
    
    for index in range(len(list)):
        if oldElement == list[index]:
            list[index] = newElement


def get_new_filepathimage():
    
    from django.conf.__init__ import settings
    
    MEDIA_ROOT = settings.MEDIA_ROOT
    number = str(random.randint(1,1000000))
    extension = '.png'
    
    filepath = os.path.join(MEDIA_ROOT, 'images/plots', number+extension)
    
    return filepath
    
    
            
def parse_attrs(attrs):
    
    attrs_parsed = []
    
    for attr in attrs:
        
        if attr:
            attr = literal_eval(attr)
            attrs_parsed.append(attr)
        

    return attrs_parsed

def parse_range(string):
    
    if string.find('-') != -1:
        
        splits = string.split('-')
        
        listed = [str(i) for i in range(int(splits[0]), int(splits[1])+1)]
        
        return literal_eval(','.join(listed))
    
    else:
        return literal_eval(string)

def parse_options(options):
    
    options_parsed = {}
    
    for option in options:
        
        if option:
            key, value = option.split('=')
            
            key = str(key.strip())
            value = literal_eval(value) 
            
            options_parsed[key] = value
    
    return options_parsed